﻿/**
* project3D Engine
* @author John Sword
* @version 2 - AS3
* 
* simple Box Primitive
*/

package engine.objects
{
	
	import engine.geom.UV;
	import engine.geom.Vector;
	import engine.geom.Vertex;
	import engine.materials.Material;

	public class Box extends Object3D
	{
		
		private var h:Number;
		private var w:Number;
		private var d:Number;
		
		/**
		* Constructor
		* @param size of the Box
		* @param position of the Box
		* @param material for the Box
		*/
		public function Box ( material:Material, size:Vector = null )
		{
			if ( !size ) size = new Vector(10,10,10);
			this.h = size.y;
			this.w = size.x;
			this.d = size.z;
			this.material = material;
			build();
		}
		
		// initialization
		private function build () : void
		{
			// build vertices
			createVertex(new Vertex (-w,h,-d));
			createVertex(new Vertex (w,h,-d));
			createVertex(new Vertex (-w,h,d));
			createVertex(new Vertex (w,h,d));
			createVertex(new Vertex (-w,-h,-d));
			createVertex(new Vertex (w,-h,-d));
			createVertex(new Vertex (-w,-h,d));
			createVertex(new Vertex (w,-h,d));
			// build faces
			createFace ( 0, 2, 3, new UV(1,0),new UV(1,1),new UV(0,1), material );
			createFace ( 3, 1, 0, new UV(0,1),new UV(0,0),new UV(1,0), material );
			createFace ( 4, 5, 7, new UV(0,0),new UV(1,0),new UV(1,1), material );
			createFace ( 7, 6, 4, new UV(1,1),new UV(0,1),new UV(0,0), material );
			createFace ( 0, 1, 5, new UV(0,0),new UV(1,0),new UV(1,1), material );
			createFace ( 5, 4, 0, new UV(1,1),new UV(0,1),new UV(0,0), material );
			createFace ( 1, 3, 7, new UV(0,0),new UV(1,0),new UV(1,1), material );
			createFace ( 7, 5, 1, new UV(1,1),new UV(0,1),new UV(0,0), material );
			createFace ( 3, 2, 6, new UV(0,0),new UV(1,0),new UV(1,1), material );
			createFace ( 6, 7, 3, new UV(1,1),new UV(0,1),new UV(0,0), material );
			createFace ( 2, 0, 4, new UV(0,0),new UV(1,0),new UV(1,1), material );
			createFace ( 4, 6, 2, new UV(1,1),new UV(0,1),new UV(0,0), material );
		}
	}
}

